package com.kbbywyclient.ui.activity;

import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;

import com.kbbywyclient.R;
import com.kbbywyclient.data.entity.ScoreResult;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.ScoreDetailModule;
import com.kbbywyclient.presenter.ScoreDetailPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.adapter.ScoreAllAdapter;
import com.kbbywyclient.ui.adapter.ViewPagerAdapter;
import com.kbbywyclient.ui.contarct.ScoreDetailContract;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.CustomToolBar;
import com.kbbywyclient.widget.GetMoreListView;
import com.kbbywyclient.widget.NoScrollViewPager;
import com.ogaclejapan.smarttablayout.SmartTabLayout;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Description:积分明细
 * Created by FHW
 * on 2017/5/8
 */

public class ScoreDetailActivity extends BaseActivity implements ScoreDetailContract.View {
    @BindView(R.id.v_toolbar)
    CustomToolBar v_toolbar;
    @BindView(R.id.stb_viewpagertab)
    SmartTabLayout stb_viewpagertab;
    @BindView(R.id.vp_viewpager)
    NoScrollViewPager vp_viewpager;

    private ViewPagerAdapter pagerAdapter;
    private List viewContainter;
    private ArrayList<String> titles;

    private int mType;


    @Inject
    ScoreDetailPresenter scoreDetailPresenter;
    private String scoreType = "";//传空字符串，查看全部  传 EYW040101 ，充值   传 EYW040103，赚取  传 EYW040102，使用

    private PtrClassicFrameLayout ptr_view_all;
    private GetMoreListView lv_score_all;
    private List<ScoreResult> scoreResultList;
    private ScoreAllAdapter scoreAllAdapter;
    private boolean isFristLoadAll = false;

    private PtrClassicFrameLayout ptr_view_recharge;
    private GetMoreListView lv_score_recharge;
    private List<ScoreResult> rec_scoreResultList;
    private ScoreAllAdapter rec_scoreAllAdapter;
    private boolean isFristLoadRec = true;

    private PtrClassicFrameLayout ptr_view_earn;
    private GetMoreListView lv_score_earn;
    private List<ScoreResult> earn_scoreResultList;
    private ScoreAllAdapter earn_scoreAllAdapter;
    private boolean isFristLoadEarn = true;

    private PtrClassicFrameLayout ptr_view_use;
    private GetMoreListView lv_score_use;
    private List<ScoreResult> use_scoreResultList;
    private ScoreAllAdapter use_scoreAllAdapter;
    private boolean isFristLoadUse = true;


    @Override
    protected int provideContentViewId() {
        return R.layout.activity_score_detail;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("积分明细");
    }

    @Override
    protected void initView() {
        scoreResultList = new ArrayList<>();
        rec_scoreResultList = new ArrayList<>();
        earn_scoreResultList = new ArrayList<>();
        use_scoreResultList = new ArrayList<>();

        v_toolbar.setLeftViewOnClickLinster(v -> finish());

        setViewPager();
        stb_viewpagertab.setViewPager(vp_viewpager);

        stb_viewpagertab.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mType = position;
                switch (position) {
                    case 0://全部
                        scoreType = "";
                        if (isFristLoadAll) {
                            ptr_view_all.autoRefresh();
                        }
                        break;
                    case 1://充值
                        scoreType = "EYW040101";
                        if (isFristLoadRec) {
                            ptr_view_recharge.autoRefresh();
                            isFristLoadRec = false;
                        }
                        break;
                    case 2://赚取
                        scoreType = "EYW040103";
                        if (isFristLoadEarn) {
                            ptr_view_earn.autoRefresh();
                            isFristLoadEarn = false;
                        }
                        break;
                    case 3://使用
                        scoreType = "EYW040102";
                        if (isFristLoadUse) {
                            ptr_view_use.autoRefresh();
                            isFristLoadUse = false;
                        }
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void setViewPager() {
        viewContainter = new ArrayList<>();
        titles = new ArrayList<>();
        titles.add("全部");
        titles.add("充值");
        titles.add("赚取");
        titles.add("使用");

        View view = LayoutInflater.from(this).inflate(R.layout.vp_score_all, null);

        ptr_view_all = (PtrClassicFrameLayout) view.findViewById(R.id.ptr_view_all);
        lv_score_all = (GetMoreListView) view.findViewById(R.id.lv_score_all);
        setViewListener(ptr_view_all, lv_score_all);
        ptr_view_all.postDelayed(() -> ptr_view_all.autoRefresh(), 100);
        // viewpager开始添加view
        viewContainter.add(view);

        View view1 = LayoutInflater.from(this).inflate(R.layout.vp_score_all, null);
        ptr_view_recharge = (PtrClassicFrameLayout) view1.findViewById(R.id.ptr_view_all);
        lv_score_recharge = (GetMoreListView) view1.findViewById(R.id.lv_score_all);
        setViewListener(ptr_view_recharge, lv_score_recharge);
        // viewpager开始添加view
        viewContainter.add(view1);

        View view2 = LayoutInflater.from(this).inflate(R.layout.vp_score_all, null);
        ptr_view_earn = (PtrClassicFrameLayout) view2.findViewById(R.id.ptr_view_all);
        lv_score_earn = (GetMoreListView) view2.findViewById(R.id.lv_score_all);
        setViewListener(ptr_view_earn, lv_score_earn);
        // viewpager开始添加view
        viewContainter.add(view2);

        View view3 = LayoutInflater.from(this).inflate(R.layout.vp_score_all, null);
        ptr_view_use = (PtrClassicFrameLayout) view3.findViewById(R.id.ptr_view_all);
        lv_score_use = (GetMoreListView) view3.findViewById(R.id.lv_score_all);
        setViewListener(ptr_view_use, lv_score_use);
        // viewpager开始添加view
        viewContainter.add(view3);

        // 设置适配器
        pagerAdapter = new ViewPagerAdapter(viewContainter, titles);
        vp_viewpager.setAdapter(pagerAdapter);

    }

    private void setViewListener(PtrClassicFrameLayout ptr, GetMoreListView lv) {
        //加载更多
        lv.setOnGetMoreListener(() -> {
            refreshAll(false);
        });

        ptr.setLastUpdateTimeRelateObject(this);
        ptr.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                lv.setHasMore();
                refreshAll(true);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });
        // the following are default settings
        ptr.setResistance(1.7f);
        ptr.setRatioOfHeaderHeightToRefresh(1.2f);
    }

    @Override
    public void initData() {

    }

    private void refreshAll(boolean isClean) {
        scoreDetailPresenter.getScoreList(isClean, PreUtils.getUser(this).getSalesID() + "", scoreType);
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getScoreDetailComponent(new ScoreDetailModule(this)).inject(this);
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadSucScore(List<ScoreResult> scoreResultList) {
        switch (mType) {
            case 0:
                ptr_view_all.refreshComplete();
                break;
            case 1:
                ptr_view_recharge.refreshComplete();
                break;
            case 2:
                ptr_view_earn.refreshComplete();
                break;
            case 3:
                ptr_view_use.refreshComplete();
                break;
        }
    }

    @Override
    public void loadFaiScore(String errorMsg) {
        switch (mType) {
            case 0:
                ptr_view_all.refreshComplete();
                lv_score_all.getMoreComplete();
                break;
            case 1:
                ptr_view_recharge.refreshComplete();
                lv_score_recharge.getMoreComplete();
                break;
            case 2:
                ptr_view_earn.refreshComplete();
                lv_score_earn.getMoreComplete();
                break;
            case 3:
                ptr_view_use.refreshComplete();
                lv_score_use.getMoreComplete();
                break;
        }
    }

    @Override
    public void setAdapterOrNotify(List<ScoreResult> mScoreResultList) {
        switch (mType) {
            case 0:
                if (scoreResultList.size() > 0) {
                    scoreResultList.clear();
                }
                scoreResultList.addAll(mScoreResultList);
                if (scoreAllAdapter == null) {
                    scoreAllAdapter = new ScoreAllAdapter(this, R.layout.item_score_all, scoreResultList);
                    lv_score_all.setAdapter(scoreAllAdapter);
                } else {
                    scoreAllAdapter.notifyDataSetChanged();
                }
                break;
            case 1:
                if (rec_scoreResultList.size() > 0) {
                    rec_scoreResultList.clear();
                }
                rec_scoreResultList.addAll(mScoreResultList);
                if (rec_scoreAllAdapter == null) {
                    rec_scoreAllAdapter = new ScoreAllAdapter(this, R.layout.item_score_all, rec_scoreResultList);
                    lv_score_recharge.setAdapter(rec_scoreAllAdapter);
                } else {
                    rec_scoreAllAdapter.notifyDataSetChanged();
                }
                break;
            case 2:
                if (earn_scoreResultList.size() > 0) {
                    earn_scoreResultList.clear();
                }
                earn_scoreResultList.addAll(mScoreResultList);
                if (earn_scoreAllAdapter == null) {
                    earn_scoreAllAdapter = new ScoreAllAdapter(this, R.layout.item_score_all, earn_scoreResultList);
                    lv_score_earn.setAdapter(earn_scoreAllAdapter);
                } else {
                    earn_scoreAllAdapter.notifyDataSetChanged();
                }
                break;
            case 3:
                if (use_scoreResultList.size() > 0) {
                    use_scoreResultList.clear();
                }
                use_scoreResultList.addAll(mScoreResultList);
                if (use_scoreAllAdapter == null) {
                    use_scoreAllAdapter = new ScoreAllAdapter(this, R.layout.item_score_all, use_scoreResultList);
                    lv_score_use.setAdapter(use_scoreAllAdapter);
                } else {
                    use_scoreAllAdapter.notifyDataSetChanged();
                }
                break;
        }
    }

    @Override
    public void setNoMore() {
        switch (mType) {
            case 0:
                lv_score_all.setNoMore();
                break;
            case 1:
                lv_score_recharge.setNoMore();
                break;
            case 2:
                lv_score_earn.setNoMore();
                break;
            case 3:
                lv_score_use.setNoMore();
                break;
        }
    }

    @Override
    public void setMoreComplete() {
        switch (mType) {
            case 0:
                lv_score_all.getMoreComplete();
                break;
            case 1:
                lv_score_recharge.getMoreComplete();
                break;
            case 2:
                lv_score_earn.getMoreComplete();
                break;
            case 3:
                lv_score_use.getMoreComplete();
                break;
        }
    }
}
